/**
 * @Author: lll
 * @Email:li.lele@vpclub.cn
 * @Date: create in 2020/7/13
 */
package cn.vpclub.admin.web.home;

import cn.vpclub.admin.entity.StarsEntity;
import cn.vpclub.admin.mapper.StarsMapper;
import cn.vpclub.admin.param.StaredResponseParam;
import cn.vpclub.admin.param.StarsRequestParam;
import cn.vpclub.admin.util.BaseResponse;
import cn.vpclub.admin.util.ResponseCodeEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping(value = "stars")
public class StarsController {
    @Autowired
    StarsMapper starsMapper;

    /**
     * 点赞
     *
     * @param starsRequestParam
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "addStars")
    public BaseResponse addStars(@RequestBody @Validated StarsRequestParam starsRequestParam, HttpServletRequest httpServletRequest) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userId", httpServletRequest.getAttribute("userId"));
        queryWrapper.eq("ArticleId", starsRequestParam.getArticleId());
        List<StarsEntity> list = starsMapper.selectList(queryWrapper);
        if (list.size() > 0) {
            return BaseResponse.info(ResponseCodeEnum.HasStared, null);
        }
        StarsEntity starsEntity = new StarsEntity();
        starsEntity.setArticleId(starsRequestParam.getArticleId());
        starsEntity.setUserId((String) httpServletRequest.getAttribute("userId"));
        starsMapper.insert(starsEntity);
        return BaseResponse.info(ResponseCodeEnum.SUCCESS, null);
    }

    /**
     * 是否点赞
     *
     * @param starsRequestParam
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "selectStars")
    public BaseResponse selectStars(@RequestBody @Validated StarsRequestParam starsRequestParam, HttpServletRequest httpServletRequest) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userId", httpServletRequest.getAttribute("userId"));
        queryWrapper.eq("ArticleId", starsRequestParam.getArticleId());
        List<StarsEntity> list = starsMapper.selectList(queryWrapper);
        return BaseResponse.info(ResponseCodeEnum.SUCCESS, list);
    }

    /**
     * 删除点赞
     *
     * @param starsRequestParam
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "deleteStars")
    public BaseResponse deleteStars(@RequestBody @Validated StarsRequestParam starsRequestParam, HttpServletRequest httpServletRequest) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userId", httpServletRequest.getAttribute("userId"));
        queryWrapper.eq("ArticleId", starsRequestParam.getArticleId());
        starsMapper.delete(queryWrapper);
        return BaseResponse.info(ResponseCodeEnum.SUCCESS, null);
    }

    /**
     * 我的点赞list
     *
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "selectStared", method = RequestMethod.POST)
    public BaseResponse selectStared(HttpServletRequest httpServletRequest) {
        List<StaredResponseParam> list = starsMapper.selectStared((String) httpServletRequest.getAttribute("userId"));
        return BaseResponse.info(ResponseCodeEnum.SUCCESS, list);
    }
}
